package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.knowledge.domain.FilesShare;
import cn.cdeden.knowledge.domain.TreeDownloadFiles;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.domain.dto.ShareDownloadFilesDto;
import cn.cdeden.knowledge.enums.TeamParamEnum;
import cn.cdeden.knowledge.service.*;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.domain.*;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.lock.annotation.Lock4j;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

/**
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.knowledge.dubbo
 * @ClassName : OssUploadShareServiceImpl
 * @Author : Chengl.
 * @Description : // 分享文件的在线预览、下载
 * @Date : 2025/02/12 16:08
 * @Version : 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "share", version = "1.0.0")
public class OssUploadShareServiceImpl implements OssUploadService {

    private final FilesShareService filesShareService;

    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        String id = info.getString("id");
        String type = info.getString("type");
        RemoteFile remoteFile = new RemoteFile();
        if (OssModuleType.Team.getCode().equals(type)) {
            //团队分享
            Map<String, Object> map = filesShareService.queryShareFileUrl(id, moduleId);
            FilesTree filesTree = (FilesTree) map.get("info");
            FilesShare filesShare = (FilesShare) map.get("shareInfo");
            remoteFile.setOssId(filesTree.getStorageId() == null ? null : Long.valueOf(filesTree.getStorageId()));
            remoteFile.setVersionId(filesTree.getEdition());
            remoteFile.setVersionName(filesTree.getEditionName());
            remoteFile.setFileSize(filesTree.getSize());
            remoteFile.setFileSuffix(filesTree.getSuffix());
            remoteFile.setName(filesTree.getRealName());
            remoteFile.setOriginalName(filesTree.getRealName());
            remoteFile.setModuleType(OssModuleType.Team.getCode());
            remoteFile.setRead(filesShare.getIsPreview() == 1);
            remoteFile.setDownload(filesShare.getIsDownload() == 1);
        }else if(OssModuleType.Personal.getCode().equals(type)){
            //个人分享
        }


        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload remoteOssUpload, Long userId) {
        return remoteOssUpload;
    }

    @Override
    @Transactional
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        return null;
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        //构建下载信息
        ShareDownloadFilesDto shareDownloadFilesDto = new ShareDownloadFilesDto();
        shareDownloadFilesDto.setId(id);
        shareDownloadFilesDto.setFileIds(idList);
        List<RemoteFilesDownload> data = filesShareService.shareDownload(shareDownloadFilesDto);
        data.forEach(remoteFilesDownload -> {remoteFilesDownload.setLibraryType("share");});
        return data;
    }
}

